package no.ntnu.fp.gui.dialog;


import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;

import no.ntnu.fp.model.Employee;
import no.ntnu.fp.utils.Participant;

@SuppressWarnings("serial")
public class Deltakere extends JPanel {
	private JList list;
	private JLabel lblDeltakere;
	private JScrollPane scrollPane;
	private JButton btnLeggTilDeltagere;
	private JButton btnFjernDelta;
	private DefaultComboBoxModel listModel;

	/**
	 * Create the panel.
	 */

	public Deltakere() {
		setLayout(null);
		setPreferredSize(new Dimension(350, 200));

		lblDeltakere = new JLabel("Deltakere:");
		lblDeltakere.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblDeltakere.setBounds(10, 11, 112, 14);
		add(lblDeltakere);

		// deltakere legges til mellom her (utseende)
		listModel = new DefaultComboBoxModel();
		list = new JList(listModel);
		scrollPane = new JScrollPane(list);
		scrollPane.setBounds(10, 36, 215, 152);
		add(scrollPane);

		btnLeggTilDeltagere = new JButton("<html>Legg til<br> deltakere");
		btnLeggTilDeltagere.setBounds(234, 36, 115, 35);
		btnLeggTilDeltagere.addActionListener(new AddParticipantButtonListener());
		add(btnLeggTilDeltagere);

		btnFjernDelta = new JButton("Fjern deltakere");
		btnFjernDelta.setBounds(234, 80, 115, 35);
		btnFjernDelta.addActionListener(new RemoveParticipantButtonListener());
		add(btnFjernDelta);
	}

	/**
	 * @see AttendigRenderer
	 * 
	 *  Participant har b�de personene og statusen p� om de deltar.
	 */
	public void setParticipants(List<Participant> participant){
		listModel = new DefaultComboBoxModel(participant.toArray());
		list.setModel(listModel);
		//		scrollPane = new JScrollPane(list);
		//		scrollPane.setBounds(10, 36, 215, 152);
		//		add(scrollPane);
	}

	public void setListCellRenderer(ListCellRenderer lcr){
		list.setCellRenderer(lcr);
	}
	public void setEditable(boolean bool){
		list.setEnabled(bool);
		lblDeltakere.setEnabled(bool);
		scrollPane.setEnabled(bool);
		btnLeggTilDeltagere.setEnabled(bool);
		btnFjernDelta.setEnabled(bool);
	}

	public List<Participant> getParticipants() {
		List<Participant> participants = new ArrayList<Participant>();
		for (int i = 0; i < listModel.getSize(); i++) {
			Participant participant = (Participant)listModel.getElementAt(i);
			participants.add(new Participant(participant.getEmployee(), null));
		}
		return participants;
	}

	private final class AddParticipantButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			ParticipantDialog participantDialog = ParticipantDialog.getInstance();
			participantDialog.show(Deltakere.this.getParent());
			if (!participantDialog.isCanceled()) {
				List<Participant> participants = new ArrayList<Participant>();
				List<Employee> emps = new ArrayList<Employee>();
				for(int i = 0; i < Deltakere.this.listModel.getSize(); i++){
					Participant p = (Participant) listModel.getElementAt(i);
					emps.add(p.getEmployee());
					participants.add(p);
				}
				for (Employee emp: participantDialog.getAllSelectedParticipants()) {
					if(!emps.contains(emp)){
						participants.add(new Participant(emp, null));
					}
				}
				setParticipants(participants);
			}
		}
	}

	private final class RemoveParticipantButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			for (Object o: list.getSelectedValues()) {
				listModel.removeElement(o);
			}
		}
	}

	public void clear() {
		listModel.removeAllElements();
	}
}